package com.richapp.launcher.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.jack.netty.log.LoggerManager;
import com.richapp.launcher.busevent.EventServiceDestroy;
import com.richapp.launcher.net.remote.RemoteClient;
import com.richapp.launcher.utils.Tools;

import de.greenrobot.event.EventBus;

public class RemoteService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        RemoteClient.getInstance().setContext(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                    RemoteClient.getInstance().start();
            }
        }).start();
        //保持服务
        Notification notification = new Notification();
        startForeground(5, notification);
        //Toast.makeText(this, "外网服务启动", Toast.LENGTH_LONG).show();
        LoggerManager.getInstance().getiLogger().info(getClass(),"外网服务器连接服务启动!");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RemoteClient.getInstance().stop();

        LoggerManager.getInstance().getiLogger().error(getClass(), "外网服务器连接服务销毁!");

        EventBus.getDefault().post(new EventServiceDestroy(RemoteService.class));
    }
}
